package rest.feign;

import feign.Feign;
import feign.gson.GsonDecoder;
import feign.gson.GsonEncoder;
import feign.jaxb.JAXBContextFactory;
import feign.jaxb.JAXBDecoder;
import feign.jaxb.JAXBEncoder;
import rest.entity.Result;
import rest.entity.User;

import java.util.Map;

public class RunSpringbootClient {

    public String targetUrl = "http://localhost:9100/";
//    public String targetUrl = "http://FAQ-Service-Provider/";
    public static void main(String[] args) {
        RunSpringbootClient runSpringbootClient = new RunSpringbootClient();
//        runSpringbootClient.callXmlUser();
//        System.out.println("------------------------------");
//        runSpringbootClient.callJsonUser();
//        System.out.println("------------------------------");
//        runSpringbootClient.callStringUser();
        runSpringbootClient.callgetUsers();
    }

    public void callXmlUser(){
        JAXBContextFactory jaxbContextFactory = new JAXBContextFactory.Builder().build();
        //获取服务接口
        SpringbootClient springbootClient = Feign.builder().
                decoder(new JAXBDecoder(jaxbContextFactory)). //解码器，将xml转化为对象
                encoder(new JAXBEncoder(jaxbContextFactory)). //编码器，将对象转化为xml
                target(SpringbootClient.class, targetUrl);
        User user = new User();
//        Member user = new Member();
        user.setUserId("123");
        user.setUserName("灵鬼");
        Result result = springbootClient.xmlUser(user);
        System.out.println(result.getMessage());
    }

    public void callJsonUser(){
        //获取服务接口
        SpringbootClient springbootClient = Feign.builder().
                decoder(new GsonDecoder()). //解码器，将json转化为对象
                encoder(new GsonEncoder()). //编码器，将对象转化为json
                target(SpringbootClient.class, targetUrl);
        User user = new User();
//        Member user = new Member();
        user.setUserId("123");
        user.setUserName("叶思");
        User result = springbootClient.jsonUser(user);
        System.out.println(result.getUserName());
        System.out.println("--------------------------");
        SpringbootClient springbootClient1 = Feign.builder().
                decoder(new GsonDecoder()). //解码器，将json转化为对象
                encoder(new GsonEncoder()). //编码器，将对象转化为json
                target(SpringbootClient.class, targetUrl);
        System.out.println(springbootClient1.jsonUserToString(user));
    }



    public void callStringUser(){
        //获取服务接口
        SpringbootClient springbootClient = Feign.builder().
                decoder(new GsonDecoder()). //解码器，将xml转化为对象
                encoder(new GsonEncoder()). //编码器，将对象转化为xml
                target(SpringbootClient.class, targetUrl);

        String result = springbootClient.stringUser("七修尊者");
        System.out.println(result);
    }

    public void callgetUsers(){
        //获取服务接口
        SpringbootClient springbootClient = Feign.builder().
                decoder(new GsonDecoder()). //解码器，将json转化为对象
                encoder(new GsonEncoder()). //编码器，将对象转化为json
                target(SpringbootClient.class, targetUrl);

//        User user = springbootClient.jsonUser(new User());
        String userId = "333,222";
        Map<String, User> users = springbootClient.getUsers(userId);
        System.out.println(users);
//        System.out.println(user.getUserName());
    }
}
